package com.wsp.controllermanagement.controller;

import com.wsp.apimanagement.pojo.dto.Result;
import com.wsp.apimanagement.pojo.entity.Admin;
import com.wsp.apimanagement.pojo.entity.Module;
import com.wsp.apimanagement.pojo.entity.Source;
import com.wsp.apimanagement.service.AdminService;
import com.wsp.apimanagement.util.CommonResultBuilderUtil;
import com.wsp.controllermanagement.Util.BcrypUtil;
import com.wsp.controllermanagement.pojo.vo.AdminViewVo;
import com.wsp.controllermanagement.validation.ValidationGroup;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * Package: com.wsp.controllermanagement.controller
 * Description： TODO
 *
 * @Author: 巫思平
 * Copyright: Copyright (c) 2020
 * Version: 1
 */
@Api(tags = "管理员操作")
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;


    /**
     * 添加管理员
     */
    @PostMapping("/addAdmin")
    public Result<String> addAdmin(@Valid @RequestBody Admin admin){
        admin.setPassword(BcrypUtil.encode(admin.getPassword()));
        adminService.insertAdmin(admin);
        return CommonResultBuilderUtil.commonSuccess("成功");
    }

    /**
     * 上传视频
     */
    @PostMapping("/addSources")
    @SuppressWarnings("unused")
    public Result<?> addSources(
            @Validated({ValidationGroup.FileGroup.class, ValidationGroup.SourceGroup.class}) AdminViewVo source
    ) throws IOException {
        //文件名
        String originalFilename = source.getFile().getOriginalFilename();
        FileInputStream fileInputStream = (FileInputStream) source.getFile().getInputStream();
        BufferedInputStream bufferedInputStream =new BufferedInputStream(fileInputStream);
        //避免恶意字符串
        String fileName = originalFilename==null? String.valueOf(System.currentTimeMillis()) :originalFilename.split("\\.")[0];
        return adminService.insertSource(source.getModuleName(), source.getSourceTitle(), bufferedInputStream, fileName);
    }

    /**
     * 上传视频封面
     */
    @PostMapping("/uploadCover")
    public Result<Source> uploadCover(
            @Validated({ValidationGroup.FileGroup.class}) AdminViewVo cover
    ) throws IOException {
        String originalFilename = cover.getFile().getOriginalFilename();
        if(cover.getSourceId()==null) {
            return null;
        }
        FileInputStream fileInputStream = (FileInputStream) cover.getFile().getInputStream();
        BufferedInputStream bufferedInputStream =new BufferedInputStream(fileInputStream);
        String fileName = originalFilename==null? String.valueOf(System.currentTimeMillis()) :originalFilename.split("\\.")[0];
        return adminService.insertCover(cover.getSourceId(),bufferedInputStream,fileName);
    }

    /**
     * 添加模块
     */
    @PostMapping("/insertModule")
    public Result<Module> insertModule(
            @Validated({ValidationGroup.ModuleGroup.class, ValidationGroup.FileGroup.class}) AdminViewVo module
    ) throws IOException {
        String originalFilename = module.getFile().getOriginalFilename();
        FileInputStream fileInputStream = (FileInputStream) module.getFile().getInputStream();
        BufferedInputStream bufferedInputStream =new BufferedInputStream(fileInputStream);
        String fileName = originalFilename==null? String.valueOf(System.currentTimeMillis()) :originalFilename.split("\\.")[0];
        return adminService.insertModule(module.getModuleName(),bufferedInputStream,fileName);
    }

    /**
     * test
     */
    @GetMapping("/test")
    public Object test(
            @Validated({ValidationGroup.ModuleGroup.class}) AdminViewVo module
    )  {
        System.out.println(module.getModuleName());
        return new Result<>(1,"111",3);
    }
}
